package com.ali.gts.servlet.controller;

import com.ali.gts.servlet.domain.Cart;
import com.ali.gts.servlet.domain.User;
import com.ali.gts.servlet.service.OrderService;
import com.ali.gts.servlet.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OrderServlet extends BaseServlet{

    OrderService orderService = new OrderServiceImpl();

    public void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取购物车对象
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        //获取登陆的用户id
        User user =(User) req.getSession().getAttribute("user");
        if(user == null){
            req.getRequestDispatcher("/client/user/login.jsp").forward(req,resp);
            return;
        }
        String userId =  user.getId();
        String orderId = orderService.createOrder(cart,userId);

        req.getSession().setAttribute("orderId",orderId);

        resp.sendRedirect("/pages/cart/checkout.jsp");

    }
}
